#!/bin/bash -eu

#
# Upload signed XPI to S3 with correct content type
#

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR="$( cd "$DIR"/../.. && pwd )"
BUILD_DIR="$( cd "$DIR"/../../build && pwd )"

. "$ROOT_DIR/config.sh"

channel=${1:-}
if [ -z "$channel" ]; then
	echo "Usage: $0 CHANNEL" >&2
	exit 1
fi

S3_PATH="$S3_BUCKET/connector/firefox/$channel/"
XPI="`$DIR/get_xpi_path`"
VERSION="$( unzip -p "$XPI" manifest.json | jq -r .version )"

if ! unzip -l "$XPI" | grep --quiet META-INF; then
	echo "$XPI is not signed"
	exit 1
fi

aws s3 cp --content-type application/x-xpinstall "$XPI" s3://${S3_PATH}Zotero_Connector-$VERSION.xpi
rm $XPI
